<template>
    <el-card class="login" v-if="handleType == 'login'">
        <div class="header">人力资源管理系统账号</div>
        <el-form :model="form" @submit.prevent="handleLogin">

            <el-form-item>
                <el-icon size="25">
                    <User />
                </el-icon>
                <el-input v-model="form.username" placeholder="请输入用户名" required></el-input>
            </el-form-item>

            <el-form-item>
                <el-icon size="25">
                    <Lock />
                </el-icon>
                <el-input show-password type="password" v-model="form.password" placeholder="请输入密码" required></el-input>
            </el-form-item>
        </el-form>
        <div class="password">
            <el-checkbox v-model="isRemenber" :checked="isRemenber" name="type" @change="rememberCode">
                记住密码
            </el-checkbox>

            <span style="color: blue;cursor: pointer;font-size: 14px;" @click="handleChangeType('forget')">忘记密码？</span>
        </div>
        <el-button type="primary" @click="handleLogin">立即登录</el-button>
        <div class="register">还没账号？<span style="color: blue;cursor: pointer;"
                @click="handleChangeType('register')">点击注册</span>
        </div>
    </el-card>
</template>
<script setup lang="ts">
import { ref, defineProps, defineEmits, onMounted } from "vue"
import { getData } from "../../../api/api"
import { useRouter } from "vue-router"
const router = useRouter()
const props = defineProps({
    handleType: String
})
const emit = defineEmits()

onMounted(() => {
    getRememberCode()
})

const handleChangeType = (type: string) => {
    //跳转前清空表单
    resetForm()
    emit("handleChangeType", type)
}
let isRemenber = ref(false)
const rememberCode = () => {
    if (isRemenber.value && form.value.username && form.value.password) {
        localStorage.setItem("form", JSON.stringify(form.value))
        localStorage.setItem("rememberCode", JSON.stringify(isRemenber.value))
    } else {
        localStorage.removeItem("form")
        localStorage.removeItem("rememberCode")
    }
}
const getRememberCode = () => {
    const rememberValue: any = localStorage.getItem("form")
    const rememberCode: any = localStorage.getItem("rememberCode")

    if (rememberValue) {
        form.value = JSON.parse(rememberValue)
    }
    if (rememberCode) {
        isRemenber.value = Boolean(rememberCode)
    }

}
let form = ref({
    username: '',
    password: ''
});

import useAccountStore from "../../../store/account"
const accountStore = useAccountStore()
const handleLogin = async () => {

    const res = await getData("/accounts", form.value)
    if (res.code != 200) return
    if (res.message == "用户名或密码错误") return alert("用户名或密码错误")
    accountStore.setUserMsg(res.account)

    router.push("/")

}

const resetForm = () => {
    form.value = {
        username: '',
        password: ''
    }
}
</script>
<style scoped lang="scss">
.el-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    padding: 20px 0;

    :deep() .el-card__body {
        padding: 0 !important;
        width: 80%;
    }

    .header {
        width: 100%;
        text-align: center;
        font-size: 20px;
        padding: 20px 0;
        letter-spacing: 3px;
    }

    .el-form {
        margin-top: 20px;

        .el-form-item {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;


            .el-input {
                width: 85%;
                margin-left: 10px;
            }

            &:not(:last-child) {
                margin-bottom: 20px;
            }
        }
    }

    .password {
        width: 100%;
        margin-bottom: 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: -10px;

        .el-checkbox {
            width: 100px;
        }
    }

    .el-button {
        width: 100%;
        height: 40px;
        letter-spacing: 3px;
        font-size: 18px;
    }

    .register {
        width: 100%;
        margin-top: 20px;
        text-align: center;
    }
}
</style>